<?php
	define('CONFIG_FILE',true);
	include '../config.php';
	
	define('DB_FILE',true);
	require_once '../Tool/DB/SqlDBManager.class.php';

	session_start();
	if (empty($_SESSION["userType"])) {
		$url="../Index.php";
		include_once '../Tool/Common/Redirect.php';
		exit();
	}

	if($_SESSION["userType"]!="System Admin"){
        $url="../Index.php";
		include_once '../Tool/Common/Redirect.php';
		exit();
    }
    
	$CentreId = $_REQUEST['CentreId'];
	$CentreName = $_REQUEST['CentreName'];
	$Address = $_REQUEST['Address'];
	$PostalCode = $_REQUEST['PostalCode'];

	$sqlDBManager = new SqlDBManager();

    $sql = "SELECT * from Centre where CentreId=?";
    $parameters = array($CentreId);
    $res=$sqlDBManager->queryRow($sql,$parameters);

    if(!empty($res)){
    	$CentreIdExisted=$res['CentreId'];
        $CentreNameExisted=$res['CentreName'];
        $AddressExisted=$res['Address'];
        $PostalCodeExisted=$res['PostalCode'];

        //If no changes, return ture else execute update statement
        if($CentreId==$CentreIdExisted&&$CentreName==$CentreNameExisted&&$Address==$AddressExisted&&$PostalCode==$PostalCodeExisted){
            echo json_encode(array('success'=>true));   
        }else{
			$sql = "UPDATE centre SET CentreName=?,Address=?,PostalCode=? WHERE CentreId=?";
			$parameters = array($CentreName,$Address,$PostalCode,$CentreId);
			$res=$sqlDBManager->execute_dml($sql,$parameters);

			//close connection
			$sqlDBManager->close_connect();

			if ($res>0){
				echo json_encode(array('success'=>true));
			}else{
				echo json_encode(array('msg'=>"Update centre failed due to database error."));
			}
		}
	}
?>